<template>
  <div style="height: 6vh;background-color:#fff;padding: 10px 20px;">
    <section id="target-section"> </section>
    <span style="line-height:60px;font-size:20px;">员工信息</span>
    <el-button type="primary" style="float: right;margin-top: 10px;"
               @click="beforeAddUser">添加新员工</el-button>
  </div>
  <!-- 新建流程弹窗 -->
  <!-- :before-close="handleClose" 在弹窗关闭前先调用handleClose方法来处理弹窗的关闭问题
   避免点击弹窗外部也能关闭弹窗-->
  <el-dialog
      :title="dialogTitle"
      style="width: 1000px;padding: 40px;"
      v-model="dialogVisible"
      :before-close="handleClose"
  >
    <h2><el-icon><List /></el-icon> 基础信息</h2>
    <el-form label-position="top" label-width="80px">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="员工姓名">
            <el-input placeholder="请输入" v-model="saveUserForm.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别">
            <el-select placeholder="请选择" v-model="saveUserForm.gender"  @change="loadUser">
              <el-option  label="男" value="1"></el-option>
              <el-option  label="女" value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="部门">
            <el-select placeholder="请选择" v-model="saveUserForm.departmentId" @change="loadUser">
              <el-option v-for="item in departmentidArr"
                         :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="岗位职称">
            <el-select placeholder="请选择" v-model="saveUserForm.level" @change="loadUser">
              <el-option v-for="item in positionNameArr"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row gutter="20">
        <el-col :span="12">
          <el-form-item label="电子邮箱">
            <el-input placeholder="请输入" v-model="saveUserForm.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码">
            <el-input placeholder="请输入" v-model="saveUserForm.phone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="员工类型">
            <el-select placeholder="请选择" v-model="saveUserForm.employeeType" @change="loadUser">
              <el-option v-for="item in employeetypeArr"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上级负责人">
            <el-select placeholder="请选择" v-model="saveUserForm.parentId"  @change="loadUser">
              <el-option v-for="item in leaderOptions"
                         :label="item.username" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <h2><el-icon><List /></el-icon> 工作状态</h2>
    <el-form label-position="top" label-width="80px">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="员工状态">
            <el-select placeholder="请选择" v-model="saveUserForm.status"  @change="loadUser">

            <el-option label="在职"  value="0"></el-option>
            <el-option label="离职"  value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker type="date" format="YYYY-MM-DD" style="width: 100%"
                            value-format="YYYY-MM-DD" v-model="saveUserForm.entryDate"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="试用期">
            <el-select placeholder="请选择" v-model="saveUserForm.probationPeriod" @change="loadUser">
              <el-option label="30"  value="30"></el-option>
              <el-option label="60"  value="60"></el-option>
              <el-option label="90"  value="90"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="转正时间" >
              <el-date-picker type="date" format="YYYY-MM-DD" style="width: 100%"
                              value-format="YYYY-MM-DD" v-model="saveUserForm.regularizationDate"/>
            </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="司龄开始时间">
            <el-date-picker type="date" format="YYYY-MM-DD" style="width: 100%"
                            value-format="YYYY-MM-DD" v-model="saveUserForm.seniorityStart"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="司龄(年)">
            <el-input v-model="saveUserForm.seniority" readonly></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveUser">保存</el-button>
    </template>
  </el-dialog>

  <!-- 用户搜索卡片 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="员工姓名" style="width: 220px;">
        <el-input placeholder="输入员工姓名" v-model="searchUserForm.username" @keydown.enter="loadUser"></el-input>
      </el-form-item>
      <el-form-item label="员工类型" style="width: 220px;">
        <el-select placeholder="请选择员工类型" v-model="searchUserForm.employeeType"  @keydown.enter="loadUser">
          <el-option v-for="item in employeetypeArr"
                       :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadUser">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
    <!-- 部门列表卡片 -->
    <!-- 审批流程卡片 -->
    <el-card style="flex: 1;">
      <el-table :data="UserArr">
        <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
        <el-table-column prop="username" label="姓名" align="center"></el-table-column>
        <el-table-column prop="gender" label="性别" align="center"
                         :formatter="genderFormate "></el-table-column>
        <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
        <el-table-column prop="email" label="邮箱" align="center"></el-table-column>
        <el-table-column prop="departmentName" label="所在部门" align="center"></el-table-column>
        <el-table-column prop="employeeType" label="员工类型" align="center"
                         :formatter="employeeArrFormatter"></el-table-column>
        <el-table-column prop="level" label="岗位职称" align="center"
                         :formatter="positionNameArrFormatter"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button link type="primary" @click="editUser(scope.row.id)">编辑</el-button>
            <el-button link type="primary" @click="deleteUser(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

//定义变量控制弹窗是否出现
const dialogVisible = ref(false);
//定义变量用来控制弹窗的标题
const dialogTitle = ref('新建审批流程');
// 用户列表
const users = ref(getUser());
//定义对象用来收集保存表单的数据
const saveUserForm = ref({
});
/* 点击"新建"按钮,出弹窗+修改弹窗标题 */
const beforeAddUser = () => {
  dialogVisible.value = true;
  dialogTitle.value = '新建审批流程';
}
//定义保存用户的方法
const saveUser = () => {
  console.log(saveUserForm.value);
  /* 安装查询字符串框架qs  npm install qs
  使用qs框架将JS对象转为查询字符串格式 name=tom&age=18,注意qs需要导入!!! import qs from "qs"; */
  let data = qs.stringify(saveUserForm.value);
  console.log(data);
  //向后端发送保存员工的请求
  /* 此处传的参数是qs转换后的查询字符串data,注意:使用了查询字符串,后端Controller不用加@RequestBody注解!!! */
  axios.post(BASE_URL + '/oa1/user/save', data)
      .then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('保存成功!');
          //关闭弹窗,并清空双向绑定的表单数据
          dialogVisible.value = false;
          saveUserForm.value = {};
          loadUser();
          //保存成功后要重新加载用户数据!
        } else {
          ElMessage.error(response.data.msg);
        }
      })
}
//处理弹窗关闭的方法
const handleClose = () => {
  if (confirm('您确认要关闭本窗口吗?')) {
    //关闭弹窗,并清空双向绑定的表单数据
    dialogVisible.value = false;
    saveUserForm.value = {};
  }
}

//定义对象用来保存查询条件
const searchUserForm = ref({
  employeeType: '',
  username: ''
});
//定义对象保存查询到的表格数据
const UserArr = ref([]);
//定义加载用户的方法
const loadUser = () => {
  let data = qs.stringify(searchUserForm.value);//username=tom&status=1
  //如果有查询条件,就根据后面拼接的条件查,没有条件默认加载所有用户数据
  axios.get(BASE_URL + '/oa1/user/select?' + data)
      .then((response) => {//回调函数中处理响应结果
        if (response.data.code == 2000) {
          UserArr.value = response.data.data;//将后端响应数据装入用户数组中
          console.log(UserArr.value);
        } else {
          ElMessage.error(response.data.msg);
        }
      })
}
//定义重置搜索条件的方法
const resetSearch = () => {
  //清空之前的搜索条件
  searchUserForm.value = {};
  //重新加载所有的用户数据
  loadUser();
}

/* 页面一加载完毕,立即执行 */
onMounted(() => {
  loadUser()
  loadDepartment()
  loadLeader()
})

//创建修改审批流程的选项
//创建删除员工的方法
const deleteUser= (id) => {
  if (confirm('您确认要删除该数据吗?')) {
    axios.post(BASE_URL + '/oa1/user/delete/' + id)
        .then((response) => {
          if (response.data.code == 2000) {
            ElMessage.success('删除成功!');
            loadUser();
          } else {
            ElMessage.error(response.data.msg);
          }
        })
  }
}
const editUser= (id) => {
  dialogVisible.value = true;
  dialogTitle.value = '修改员工信息';
  axios.get(BASE_URL + '/oa1/user/select?id=' + id).then((response) => {
    if (response.data.code == 2000) {
      saveUserForm.value = response.data.data[0];
      //拿到当前选择的员工职级,当前员工的职级+10才是所有可选直属领导的职级
      let level = parseInt(saveUserForm.value.level)+5;
      axios.get(BASE_URL+'/oa1/user/select?level='+level)
          .then((response)=>{
            if(response.data.code == 2000){
              console.log(response.data.data);
              leaderOptions.value = response.data.data;
            }else{
              ElMessage.error(response.data.msg);
            }
          })
      dialogVisible.value = true;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义对象用来保存查询条件
const searchDepartment= ref({departmentModule:'', status:'',name:''});
//定义对象保存查询到的表格数据
const departmentidArr = ref([
]);
//定义加载用户的方法
const loadDepartment = ()=>{
  console.log(departmentidArr)
  console.log("加载.....")
  let data = qs.stringify(searchDepartment.value);//username=tom&status=1
  //如果有查询条件,就根据后面拼接的条件查,没有条件默认加载所有用户数据
  axios.get(BASE_URL+'/oa1/department/select?'+data)
      .then((response)=>{//回调函数中处理响应结果
        if(response.data.code == 2000){
          departmentidArr.value = response.data.data;//将后端响应数据装入用户数组中
          console.log(departmentidArr.value);
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
const genderArr=ref([]);
const genderFormate = (row,column,cellValue,index)=>{
    if(cellValue==0){
      cellValue = '女';
    }else if(cellValue=1){
      cellValue = '男';
  }
  return cellValue;
}


onMounted(()=>{
  loadDictOption(employeetypeArr,'employee_type');
  loadDictOption(positionNameArr,'position_name');
})
const employeetypeArr= ref([]);
const employeeArrFormatter = (row,column,cellValue,index)=>{
  if(!employeetypeArr.value){
    return cellValue;
  }
  for(let item of employeetypeArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
const positionNameArr= ref([]);
const positionNameArrFormatter = (row,column,cellValue,index)=>{
  if(!positionNameArr.value){
    return cellValue;
  }
  for(let item of positionNameArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
//定义数组用来维护员工直属领导的选项
const leaderOptions = ref([
]);
const loadLeader = ()=>{
  //优化:如果已经选择了直属领导数据,需要再次重新选择职级的话
  //就需要将之前已加载的领导数据和已选择的直属领导数据清空!
  leaderOptions.value = [];
  saveUserForm.value.parentId = '';
  //拿到当前选择的员工职级,当前员工的职级+10才是所有可选领导的职级
  let level = parseInt(saveUserForm.value.level)+5;
  axios.get(BASE_URL+'/oa1/user/selectlevel?level='+level)
      .then((response)=>{
        if(response.data.code == 2000){
          leaderOptions.value = response.data.data;
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
//定义校验购买时间与上牌时间的先后顺序方法

</script>

<style scoped>
</style>